<?php

namespace App\Store\Controllers\Wxapp\WxappGift;

use App\Common\Services\Store\Common as StoreCommon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Wxapp\Gift\WxappGiftLog;
use Maatwebsite\Excel\Facades\Excel;
use App\Common\Services\CommonExcelExport;
/**
 * 礼品流水
 */
class WxappGiftLogController extends Controller
{
    /**
     * 列表
     */
    public function getlist(Request $request){
        $list=WxappGiftLog::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 导出
     */
    public function doexport(Request $request)
    {
        $list = WxappGiftLog::getList($request)['list'];
        $exceldata = [];
        //1采购入库，2门店入库，3门店调拨，4门店销售
        $type_arr=['1'=>'采购入库','2'=>'门店入库','3'=>'门店调拨','4'=>'门店销售'];
        $row = [
            ['table_header'=>'礼品流水'],
            [
                // 'store_name' => '所属门店',
                'start_store_name' => '起始门店',
                'target_store_name' => '目的门店',
                'gift_name' => '礼品名称',
                'count'=>"数量",
                'type' => '类型',
                'create_at' => '创建时间',
            ]
        ];
        foreach ($list as $val) {
            $exceldata[] = [
                'store_name' => $val['store_info']?$val['store_info']['store_name']:'',
                'start_store_name' => $val['start_store_info']?$val['start_store_info']['store_name']:'',
                'target_store_name' => $val['target_store_info']?$val['target_store_info']['store_name']:'',
                'gift_name'=> $val['gift_name'],
                'count' => $val['count'],
                'type' => $type_arr[$val['type']],
                'create_at'=>date('Y-m-d H:i:s',$val['create_at'])
            ];
        }

        $filename = '礼品流水信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
